package com.lx.boot.web.valid;

import com.lx.annotation.Note;
import com.lx.boot.OS;
import com.lx.entity.Var;
import com.lx.util.LX;

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

public interface CheckValidatorFunctions {

    @Note("ext 参数指定的字段不能全部为空! 注解需作用于实体上")
    public class NotAllEmpty implements Function<Validate, Boolean>{
        @Override
        public Boolean apply(Validate validate) {
            String[] fields = validate.getExt();
            Var map = LX.toMap(validate.getObject());
            for (String filedName : fields) {
                if (LX.isNotEmpty(map.get(filedName))){
                    return true;
                }
            }
            return false;
        }
    }

    @Note("ext 参数指定的类型的UserType不能为空!")
    public class NotEmptyByUserTypes implements Function<Validate, Boolean>{
        @Override
        public Boolean apply(Validate validate) {
            //参数不为空直接放行
            if (LX.isNotEmpty(validate.getObject())){
                return true;
            }
            List<String> list = Arrays.asList(validate.getExt());
            // 当前用户类型不在指定范围,可以为空.
            return !list.contains(OS.getUserType());
        }
    }

    @Note("ext 参数指定的类型的UserType才可以为空!")
    public class CanEmptyByUserTypes implements Function<Validate, Boolean>{
        @Override
        public Boolean apply(Validate validate) {
            //参数不为空直接放行
            if (LX.isNotEmpty(validate.getObject())){
                return true;
            }
            List<String> list = Arrays.asList(validate.getExt());
            // 当前用户类型在指定范围可以为空,否则报错
            return list.contains(OS.getUserType());
        }
    }

}
